//
//  LPCostomLabelAndTextViewAdaptHeightView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPCostomLabelAndTextViewAdaptHeightView.h"

@implementation LPCostomLabelAndTextViewAdaptHeightView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title textViewHeight:(CGFloat)height
{
    self = [super initWithFrame:frame];
    if (self) {
        self.adaptLabel = [[UILabel alloc] init];
        self.adaptLabel.text = title;
        self.adaptLabel.textColor = [UIColor textBlackColor];
        self.adaptLabel.font = LYPFONT(15);
        [self addSubview:self.adaptLabel];
        self.adaptLabel.sd_layout
        .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .topEqualToView(self)
        .heightIs(20 * [UIScreen zj_screenHScale])
        .widthIs(180);
        
        self.adaptTextView = [UITextView new];
        self.adaptTextView.layer.borderWidth = 1;
        self.adaptTextView.layer.borderColor = [UIColor zj_colorWithHexString:@"ebebeb"].CGColor;
        self.adaptTextView.returnKeyType = UIKeyboardTypeTwitter;
        self.adaptTextView.delegate = self;
        self.adaptTextView.layer.cornerRadius = 3;
        self.adaptTextView.layer.masksToBounds = YES;
        self.adaptTextView.textColor = [UIColor textGrayColor];
        self.adaptTextView.font = LYPFONT(15);
        [self addSubview:self.adaptTextView];
        self.adaptTextView.sd_layout
        .topSpaceToView(self.adaptLabel, 20 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.adaptLabel)
        .heightIs(height)
        .widthIs([UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale]);
        
    }
    return self;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        
        return NO;
        
    }
    
    NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (self.maxNumber != 0 && toBeString.length >= self.maxNumber) {
        textView.text = [toBeString substringToIndex:self.maxNumber];
        return NO;
    }

    
    return YES;    
    
}

@end
